﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Web;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using OneServiceMultiBinding;

namespace AttributeTest
{
    class Program
    {public static Type BuildTypeWithCustomAttributesOnMethod()        {            
        AppDomain currentDomain = Thread.GetDomain();             
        AssemblyName myAsmName = new AssemblyName();            
        myAsmName.Name = "MyAssembly";            
        //动态创建一个程序集           
        AssemblyBuilder myAsmBuilder = currentDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);             
        //动态创建一个模块            
        ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule("MyModule");            
        //动态创建一个类型：MyType        
        TypeBuilder myTypeBuilder = myModBuilder.DefineType("MyType", TypeAttributes.Public);            
        //定义构造器参数           
        Type[] ctorParams = new Type[] { typeof(string) };          
        //获取构造器信息            
        ConstructorInfo classCtorInfo = typeof(WebGetAttribute).GetConstructor(ctorParams);           
        //动态创建ClassCreatorAttribute           
        CustomAttributeBuilder myCABuilder = new CustomAttributeBuilder(                
            classCtorInfo,                           new object[] { "Joe Programmer" });       
        //将上面动态创建的Attribute附加到(动态创建的)类型MyType        
        myTypeBuilder.SetCustomAttribute(myCABuilder);            
        //动态创建一个无返回值，无参数的，公有方法HelloWorld    
        MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("HelloWorld", MethodAttributes.Public, null, new Type[] { });      
        ctorParams = new Type[] { typeof(string) };           
        //获取DateLastUpdatedAttribute的构造函数信息         
        classCtorInfo = typeof(WebGetAttribute).GetConstructor(ctorParams);       
        //动态创建DateLastUpdatedAttribute           
        CustomAttributeBuilder myCABuilder2 = new CustomAttributeBuilder(                 
            classCtorInfo,                           new object[] { DateTime.Now.ToString() });       
        //将上面动态创建的Attribute附加到(动态创建的)方法HelloWorld          
        myMethodBuilder.SetCustomAttribute(myCABuilder2);        
        ILGenerator myIL = myMethodBuilder.GetILGenerator();   
        myIL.EmitWriteLine("Hello, world!");//在HelloWorld方法中，创建一行等效于Console.Write("Hello,world!");的代码       
        myIL.Emit(OpCodes.Ret);//HelloWorld方法的return语句             
        return myTypeBuilder.CreateType();         }
        static void Main(string[] args)
        {
            WebGetAttribute s = new WebGetAttribute();
            Type tt = typeof(IService1);
            MethodInfo mi = tt.GetMethods()[0];
            //MethodBuilder myMethodBuilder = MethodBuilder.GetCurrentMethod();
           // myMethodBuilder.

        }
        /*
         // Declare a delegate that will be used to execute the completed
    // dynamic method. 
    private delegate int HelloInvoker(string msg, int ret);

    public static void Main()
    {
        // Create an array that specifies the types of the parameters
        // of the dynamic method. This method has a string parameter
        // and an int parameter.
        Type[] helloArgs = {typeof(string), typeof(int)};

        // Create a dynamic method with the name "Hello", a return type
        // of int, and two parameters whose types are specified by the
        // array helloArgs. Create the method in the module that
        // defines the Test class.
        DynamicMethod hello = new DynamicMethod("Hello", 
            typeof(int), 
            helloArgs, 
            typeof(Test).Module);

        // Create an array that specifies the parameter types of the
        // overload of Console.WriteLine to be used in Hello.
        Type[] writeStringArgs = {typeof(string)};
        // Get the overload of Console.WriteLine that has one
        // String parameter.
        MethodInfo writeString = 
            typeof(Console).GetMethod("WriteLine", writeStringArgs);

        // Get an ILGenerator and emit a body for the dynamic method.
        ILGenerator il = hello.GetILGenerator();
        // Load the first argument, which is a string, onto the stack.
        il.Emit(OpCodes.Ldarg_0);
        // Call the overload of Console.WriteLine that prints a string.
        il.EmitCall(OpCodes.Call, writeString, null);
        // The Hello method returns the value of the second argument;
        // to do this, load the onto the stack and return.
        il.Emit(OpCodes.Ldarg_1);
        il.Emit(OpCodes.Ret);

        // Create a delegate that represents the dynamic method. This
        // action completes the method, and any further attempts to
        // change the method will cause an exception.
        HelloInvoker hi = 
            (HelloInvoker) hello.CreateDelegate(typeof(HelloInvoker));

        // Use the delegate to execute the dynamic method. Save and
        // print the return value.
        int retval = hi("\r\nHello, World!", 42);
        Console.WriteLine("Executing delegate hi(\"Hello, World!\", 42) returned {0}",
            retval);

        // Do it again, with different arguments.
        retval = hi("\r\nHi, Mom!", 5280);
        Console.WriteLine("Executing delegate hi(\"Hi, Mom!\", 5280) returned {0}",
            retval);

        // Create an array of arguments to use with the Invoke method.
        object[] invokeArgs = {"\r\nHello, World!", 42};
        // Invoke the dynamic method using the arguments. This is much
        // slower than using the delegate, because you must create an
        // array to contain the arguments, and ValueType arguments
        // must be boxed.
        object objRet = hello.Invoke(null, invokeArgs);
        Console.WriteLine("hello.Invoke returned {0}", objRet);
    }
}
*/
    }
}
